package com.ruoyi.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.blog.domain.BlogCategory;
import com.ruoyi.blog.domain.vo.BlogArticleVo;
import com.ruoyi.blog.service.BlogArticleLabelService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.blog.domain.bo.BlogLabelBo;
import com.ruoyi.blog.domain.vo.BlogLabelVo;
import com.ruoyi.blog.domain.BlogLabel;
import com.ruoyi.blog.mapper.BlogLabelMapper;
import com.ruoyi.blog.service.IBlogLabelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 博客标签Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-01
 */
@RequiredArgsConstructor
@Service
public class BlogLabelServiceImpl extends ServiceImpl<BlogLabelMapper, BlogLabel> implements IBlogLabelService {

    private final BlogLabelMapper baseMapper;

    /**
     * 查询博客标签
     */
    @Override
    public BlogLabelVo queryById(Long labelId){
        return baseMapper.selectVoById(labelId);
    }

    /**
     * 查询博客标签列表
     */
    @Override
    public TableDataInfo<BlogLabelVo> queryPageList(BlogLabelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogLabel> lqw = buildQueryWrapper(bo);
        Page<BlogLabelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<BlogLabelVo> getArticleLabels(BlogArticleVo n) {
        List<BlogLabel> list = baseMapper.selectListByArticle(n);
        if (CollUtil.isEmpty(list)) {
            return CollUtil.newArrayList();
        }
        return BeanCopyUtils.copyList(list, BlogLabelVo.class);
    }

    /**
     * 查询博客标签列表
     */
    @Override
    public List<BlogLabelVo> queryList(BlogLabelBo bo) {
        LambdaQueryWrapper<BlogLabel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogLabel> buildQueryWrapper(BlogLabelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogLabel> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLabel()), BlogLabel::getLabel, bo.getLabel());
        lqw.eq(bo.getIsShow() != null, BlogLabel::getIsShow, bo.getIsShow());
        lqw.orderBy(bo.getOrderBy() != null, bo.getOrderBy() != null && bo.getOrderBy() == 0,
            BlogLabel::getOrderBy, BlogLabel::getCreateTime);
        return lqw;
    }

    /**
     * 新增博客标签
     */
    @Override
    public Boolean insertByBo(BlogLabelBo bo) {
        BlogLabel add = BeanUtil.toBean(bo, BlogLabel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLabelId(add.getLabelId());
        }
        return flag;
    }

    /**
     * 修改博客标签
     */
    @Override
    public Boolean updateByBo(BlogLabelBo bo) {
        BlogLabel update = BeanUtil.toBean(bo, BlogLabel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogLabel entity){
        //TODO 做一些数据校验,如唯一约束
        List<BlogLabel> list = lambdaQuery().eq(BlogLabel::getLabel,
            entity.getLabel()).list();
        if (list.size() == 1 && entity.getLabelId() == null ||
            list.size() == 1 && !list.get(0).getLabelId().equals(entity.getLabelId())){
            throw new ServiceException("标签已存在");
        }
    }

    /**
     * 批量删除博客标签
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
